<?php
/**
 * @author shutyuka@gmail.com
 * @modified 2011-02-20
 */
class DeviceUse{
	
	static public $FLAG_INACTIVE = 0;
	
	static public $FLAG_UNACTIVE = 10;
	
	static public $TABLE_NAME="device_use";

	static public function Create($deviceUse) {
		$deviceUse['create_time'] = time();
		$deviceUse['id'] = DB::Insert(self::$TABLE_NAME, $deviceUse);
		
		return $deviceUse['id'];
	}
	
	static public function Update($deviceUse) {
		Table::UpdateCache(self::$TABLE_NAME, $deviceUse['id'], $deviceUse);
		
		return ;
	}
	
	static public function GetListByShopIdAndServiceId($shopId, $serviceId, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute) {
		$sql = "select devices.id, devices.device_name from devices, device_use where "
		."devices.shop_id=$shopId and device_use.device_id=devices.id and device_use.service_id=$serviceId ";
		return DB::GetQueryResult($sql, false);
	}
	
	static public function GetServicesById($deviceId) {
		$sql = " select services.id as id, services.service_name as service_name, device_use.skill_level as skill_level from device_use, services where "
		."  device_use.device_id=$deviceId and device_use.service_id=services.id ";
		return DB::GetQueryResult($sql, false);
	}
	
	static public function GetByDeviceAndSkill($deviceId, $skillId) {
		$oc = array("device_id"=>$deviceId, "service_id"=>$skillId);
		return DB::LimitQuery(self::$TABLE_NAME, array( "condition" => $oc ));
	}
	
	static public function GetDevicesBySkillAndLevel($serviceId, $skillLevel) {
		$oc = array("skill_level"=>$skillLevel, "service_id"=>$serviceId);
		return DB::LimitQuery(self::$TABLE_NAME, array( "condition" => $oc ));
	}
	
	static public function Delete($deviceId, $serviceId) {
		$sql = " delete from device_use where device_id=$deviceId and service_id=$serviceId ";
		return DB::Query($sql, false);
	}
	
	static public function DeleteById($deviceId) {
		$sql = " delete from device_use where device_id=$deviceId";
		return DB::Query($sql, false);
	}
	
}
